//
//  HYRootViewController.m
//  qiJi
//
//  Created by 凤利史 on 2018/3/26.
//  Copyright © 2018年 sunrose. All rights reserved.
//

#import "HYRootViewController.h"
#import "HYRootLunBoCollectionViewCell.h"
#import "HYRootCollectionReusableView.h"
#import "HYRootSecondCollectionViewCell.h"
#import "HYRootFirstCollectionViewCell.h"
#import "HYRootXiaoXiZhongXinViewController.h"
#import "HYRootXiangQingViewController.h"
#import "HYFaBuQiYouQuanViewController.h"//发布
@interface HYRootViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UICollectionView *huYuCollectionView;
@property(nonatomic,strong) UIScrollView *sxssScrollView;
@end

@implementation HYRootViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = CLColor(250, 250, 250);
    [self creationView];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    [self leftNavHide];
    self.title = @"互娱";
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, 1, DEF_SCREEN_WIDTH, 1)];
    threadView.backgroundColor = getColor(@"E6E6E6");
    [self.view addSubview:threadView];
    //右边item
    UIButton *searchBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(40), screenScale(36))];
    [searchBtn setImage:[UIImage imageNamed:@"dynamic_issue_btn"] forState:UIControlStateNormal];
    [searchBtn addTarget:self action:@selector(pianoBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightItme = [[UIBarButtonItem alloc]initWithCustomView:searchBtn];
    self.navigationItem.rightBarButtonItem = rightItme;
    
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, screenScale(38), screenScale(34))];
    [leftBtn setImage:[UIImage imageNamed:@"dynamic_message_btn"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarBtn;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
//    layout.itemSize = CGSizeMake(screenScale(290), screenScale(290));
//    layout.minimumLineSpacing = screenScale(20);
//    layout.minimumInteritemSpacing = screenScale(20);
    
    _huYuCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 1, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - (KIsiPhoneX?84:64) - 50) collectionViewLayout:layout];
    _huYuCollectionView.showsVerticalScrollIndicator = NO;
    _huYuCollectionView.delegate = self;
    _huYuCollectionView.dataSource = self;
    _huYuCollectionView.backgroundColor = [UIColor whiteColor];
    
    [_huYuCollectionView registerClass:[HYRootLunBoCollectionViewCell class] forCellWithReuseIdentifier:@"collectIdentifier"];
    
    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HYRootCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
    
    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HYRootSecondCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HYRootSecondCollectionViewCell"];
    [_huYuCollectionView registerNib:[UINib nibWithNibName:@"HYRootFirstCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"HYRootFirstCollectionViewCell"];
    \
    [self.view addSubview:_huYuCollectionView];
    
    
    
    
    
}

#pragma mark -------------------- collectionView ----------------------
//创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {
    if (kind == UICollectionElementKindSectionHeader) {
        
        HYRootCollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        
        if (indexPath.section == 1) {
            reusableView.titleLab.text = @"热门话题";
        }else {
            reusableView.titleLab.text = @"骑友圈";
        }
        

        return reusableView;
    }
    return nil;
    
    
    
}
#pragma mark --- UICollectionViewDelegate ---
//确定section的个数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{
    
    return 3;
    
}
// 设置section头视图的参考大小，与tableheaderview类似
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return CGSizeMake(0, 0);
        
    }
    else {
        return CGSizeMake(DEF_SCREEN_WIDTH, 30);
    }
}

//设置每个 UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return CGSizeMake(DEF_SCREEN_WIDTH, 175);
    }else if (indexPath.section ==2){
        
        return CGSizeMake((DEF_SCREEN_WIDTH-40)/2, 276);
    }else{
        
        return CGSizeMake(DEF_SCREEN_WIDTH, 93);
        
    }
    
}

//定义每个UICollectionView 的间距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 2) {
        return UIEdgeInsetsMake(15, 15, 15, 15);
    }else{
        return UIEdgeInsetsMake(0, 0, 0, 0);//上、左、下、右
    }
    
}

//定义每个UICollectionView 的横向间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10;
}


-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 2) {
        
        HYRootXiangQingViewController *vc = [[HYRootXiangQingViewController alloc] init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
        NSLog(@"看看是什么%ld", (long)indexPath.row);
        
        
    }
   
}

#pragma mark --- UICollectionViewDataSource ---
//确定每个section对应的item的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 2) {
        return 12;
    }else{
        return 1;
        
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identify = @"collectIdentifier";
        HYRootLunBoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
        
        return cell;
    }else if (indexPath.section == 1){
        
        HYRootFirstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYRootFirstCollectionViewCell" forIndexPath:indexPath];
        self.sxssScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 15, DEF_SCREEN_WIDTH, 68)];
        self.sxssScrollView.contentSize=CGSizeMake(115*5 + 10 * 5,68);
        
        self.sxssScrollView.backgroundColor=[UIColor whiteColor];
        self.sxssScrollView.showsHorizontalScrollIndicator=NO;
        self.sxssScrollView.pagingEnabled = NO;
        
        
        [cell addSubview:self.sxssScrollView];
        
        // [self.sxssScrollView removeAllSubviews];
        
        for (int i=0; i<5; i++) {
            UIButton *buttonOfShool=[[UIButton alloc]initWithFrame:CGRectMake((115)*i+(10)*(i+1), 0, 115, 68)];
            
            [buttonOfShool addTarget:self action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            buttonOfShool.tag = i;
            
            //  [cell.sxScrollView addSubview:BTN];
            [self.sxssScrollView addSubview:buttonOfShool];
            
            
            UIImageView *picImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0,(115), 68)];
            
            picImageView.image = [UIImage imageNamed:@"3479.jpg_wh1200"];
            

            [buttonOfShool addSubview:picImageView];

            
 
        }
        
        
        return cell;
    }else{
        
        HYRootSecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HYRootSecondCollectionViewCell" forIndexPath:indexPath];
        return cell;
        
        
    }
    
}



//右边发布
- (void)pianoBtnClick{
    HYFaBuQiYouQuanViewController *vc = [[HYFaBuQiYouQuanViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
//左边消息
- (void)leftBtnClick{
    HYRootXiaoXiZhongXinViewController *vc = [[HYRootXiaoXiZhongXinViewController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
- (void)myBtnClick:(UIButton *)sender{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
